package com.valuegroup.smarttodo.data.service;

import android.text.TextUtils;

import com.valuegroup.smarttodo.data.model.AppStatus;
import com.valuegroup.smarttodo.data.model.Rethink;
import com.valuegroup.smarttodo.data.model.RethinkDao;

import java.util.Date;
import java.util.List;

public class RethinkService extends BaseService {

    public List<Rethink> findRethink(Long pid) {

        /*if (getRethinkDao().loadAll().size() == 0) {
            for (int i = 0; i < 10; i++) {
                Rethink r = getDefaultRethink();
                r.setTitle("反思项" + i);
                getRethinkDao().save(r);

                for (int j = 0; j < 5; j++) {
                    Rethink c = getDefaultRethink();
                    c.setTitle("反思项明细" + j);
                    c.setPid(r.getId());
                    getRethinkDao().save(c);
                }
            }
        }*/

        return getRethinkDao().queryBuilder()
                .where(RethinkDao.Properties.Pid.eq(pid))
                .where(RethinkDao.Properties.Usable.eq(true))
                .where(RethinkDao.Properties.Status.eq(AppStatus.Status.NORMAL))
                .orderAsc(RethinkDao.Properties.Sort)
                .list();
    }

    public List<Rethink> findRethink() {
        return findRethink(0L);
    }

    public Rethink getDefaultRethink() {
        Rethink t = new Rethink();
        t.setPid(0L);
        t.setTitle("内容");
        t.setUsable(true);
        t.setSort(0);
        t.setCreatedDt(new Date());
        t.setUpdatedDt(t.getCreatedDt());
        t.setStatus(AppStatus.Status.NORMAL);
        return t;
    }

    public void updateTitle(Long id, String content) {
        Rethink t = getRethinkDao().load(id);
        if (t == null) {
            return;
        }

        t.setTitle(content);
        t.setUpdatedDt(new Date());
        getRethinkDao().update(t);
    }

    public void multiInsert(String content) {

        String[] arr = content.split("#");
        for (String s : arr) {
            String[] childArr = s.split("@");
            if (childArr.length > 0 && !TextUtils.isEmpty(childArr[0].trim())) {
                Rethink parent = getDefaultRethink();
                parent.setTitle(childArr[0].trim());
                getRethinkDao().save(parent);

                if (childArr.length > 1) {
                    for (int i = 1; i < childArr.length; i++) {
                        if (!TextUtils.isEmpty(childArr[i])) {

                            Rethink child = getDefaultRethink();
                            child.setTitle(childArr[i].trim());
                            child.setPid(parent.getId());
                            child.setSort(i);
                            getRethinkDao().save(child);
                        }
                    }
                }
            }
        }
    }

    public void delRethink(Long id) {
        Rethink t = getRethinkDao().load(id);
        if (t == null) {
            return;
        }

        // 删除明细
        List<Rethink> child = findRethink(t.getId());
        getRethinkDao().deleteInTx(child);

        getRethinkDao().delete(t);
    }
}
